package com.tsd.core.config.session;

import com.tsd.core.utils.HlpUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: SpringSessionContex
 * @Description: TODO
 * @Author: Luoyc
 * @Date: 2021/1/9 13:29
 * @Version: 1.0
 */
public class SpringSessionContext {
    private static final Map<String, HttpSession> SESSION_MAP = new HashMap<>();

    public static synchronized void addSession(HttpSession session) {
        if (session != null) {
            SESSION_MAP.put(session.getId(), session);
        }
        System.out.println("======SESSION_MAP.size()：" + SESSION_MAP.size());
    }

    public static synchronized void removeSession(HttpSession session) {
        if (session != null) {
            SESSION_MAP.remove(session.getId());
        }
    }

    public static synchronized void removeSession(String sessionId) {
        if (sessionId != null) {
            SESSION_MAP.remove(sessionId);
        }
    }

    public static synchronized HttpSession getSession(String sessionId) {
        if (!HlpUtils.isEmpty(sessionId)) {
            return SESSION_MAP.get(sessionId);
        }
        return null;
    }
}
